#include "CTurret.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers\CSGD_Direct3D.h"
#include "Messaging\CSGD_EventSystem.h"
#include "Messaging\CSGD_MessageSystem.h"
#include "CBullet.h"
#include "SGD Wrappers/CSGD_FModManager.h"

CTurret::CTurret()
{
	m_fCurrentRotation = 0.0f;
	m_fDistanceToTarget = 0.0f;
	m_Target = NULL;
	m_nWidth = 32;
	m_nHeight = 32;
	m_nType = OBJ_TURRET;
	m_fVelX = 0.0f;
	m_fVelY = 0.0f;

	m_tFacingDir.fX = 0.0f;
	m_tFacingDir.fY = -100.0f;

	m_tVec2Ship.fX = 0.0f;
	m_tVec2Ship.fY = 0.0f;
	m_nLazerShot = -1;

	CSGD_EventSystem::GetInstance()->RegisterClient("firebullet", this);
}
void CTurret::SetTarget(CBase* _target)
{
	if(m_Target)
	m_Target->Release();
	m_Target = _target;
	if(m_Target)
		m_Target->AddRef();
}
CTurret::~CTurret()
{
	if(m_Target)
		m_Target->Release();

	CSGD_EventSystem::GetInstance()->UnregisterClient("firebullet", this);
}

void CTurret::Update(float _fElapsedTime)
{
	if(m_Target)
	{
		tVector2D turretShip;
		turretShip.fX = m_Target->GetPosX() - m_fPosX;
		turretShip.fY = m_Target->GetPosY() - m_fPosY;

		m_tVec2Ship.fX = m_Target->GetPosX() - m_fPosX;
		m_tVec2Ship.fY = m_Target->GetPosY() - m_fPosY;

		tVector2D halfSpace;
		halfSpace.fX = 100.0f;
		halfSpace.fY = 0.0f;
		float dot = DotProduct(turretShip,halfSpace);

		m_fCurrentRotation = AngleBetweenVectors(m_tFacingDir, turretShip);
		if(dot<0)
			m_fCurrentRotation*=-1;
	}
}

void CTurret::Render()
{
	tVector2D turretShip;
	turretShip.fX = m_Target->GetPosX() - m_fPosX;
	turretShip.fY = m_Target->GetPosY() - m_fPosY;

	RECT baseRect = {0,0,32,32};
	CSGD_TextureManager::GetInstance()->Draw(GetTexID(),(int)m_fPosX,(int)m_fPosY, 1.0f, 1.0f, &baseRect);
	
	RECT turretRect = {32,0,64,32};
	CSGD_TextureManager::GetInstance()->Draw(GetTexID(),(int)m_fPosX,(int)m_fPosY-11, 1.0f, 1.0f, &turretRect, m_nWidth*0.5f, m_nHeight*0.5f+11, m_fCurrentRotation);
	
	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	CSGD_Direct3D::GetInstance()->DrawLine(int(m_fPosX + m_nWidth*0.5f), int(m_fPosY + m_nHeight*0.5f), int(m_fPosX + m_nWidth*0.5f + m_tVec2Ship.fX), int(m_fPosY + m_nHeight*0.5f+ m_tVec2Ship.fY));

	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	char buffer[128]= {0};
	float angleDeg = D3DXToDegree(m_fCurrentRotation);
	if(angleDeg < 0)
		angleDeg = 360 + angleDeg;

	sprintf_s(buffer, _countof(buffer), "Rotation:  %.1f", angleDeg);
	CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 10, 20);
	sprintf_s(buffer, _countof(buffer), "Distance:  %.1f", Vector2DLength(turretShip));
	CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 10, 40);
}

bool CTurret::CheckCollision(IBaseInterface *_pIBase)
{
	bool hit = CBase::CheckCollison(_pIBase);

	if(hit)
	{
		if(_pIBase->GetType() == OBJ_BULLET)
		{
			CBullet* bullet =(CBullet*) _pIBase;

			if(bullet->GetOwner() != this)
			{
				CSGD_MessageSystem::GetInstance()->SendMsg(new CDestroyTurretMessage(this));
				CSGD_EventSystem::GetInstance()->SendEvent("turretdestroyed", this);
			}
		}
	}
	
	return hit;
}

RECT CTurret::GetCollisonRect()
{
	RECT collisonRect = {(long)m_fPosX,(long)m_fPosY,(long)m_fPosX + m_nWidth/2,(long)m_fPosY+ m_nHeight/2};

	return collisonRect;
}

void CTurret::HandleEvent(CEvent* pEvent)
{
	if (pEvent->GetEventID() == "firebullet")
	{
		//if( ! CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nLazerShot))
			//CSGD_FModManager::GetInstance()->PlaySoundA(m_nLazerShot);

		CSGD_MessageSystem::GetInstance()->SendMsg(new CCreateBulletMessage(this));
	}

}